﻿using System;
using System.Collections.Generic;
using System.Linq;
using Medianamik.Core;
using Medianamik.Core.Caching;
using Moq;
using NUnit.Framework;
using Medianamik.Core.Interfaces;

namespace Medianamik.Tests.Unit.Medianamik.Core
{
    [TestFixture]
    public  class NodeManagerTest
    {
        [Test]
        public void When_getting_node_that_does_not_exist_it_should_return_null()
        {
            CreateExistingNodes(Enumerable.Empty<Node>());

            var node = NodeManager.GetNode<NodeWrapper>(Guid.NewGuid());

            Assert.That(node, Is.Null);

        }

        [Test]
        public void When_getting_node_that_exist_it_should_return_wrapped_node()
        {
            var expectedNode = new Node();
            expectedNode.NamePropertyId = Guid.NewGuid();

            CreateExistingNodes(new[] { expectedNode });

            var actualNodeWrapper = NodeManager.GetNode<NodeWrapper>(Guid.NewGuid());

            Assert.That(actualNodeWrapper.InnerNode, Is.EqualTo(expectedNode));
        }

        private void CreateExistingNodes(IEnumerable<Node> expectedNodes)
        {
            var cacheProvider = new Mock<INodeCacheProvider>();

            cacheProvider.Setup(x => x.GetNodes(It.IsAny<IQueryDefinition>(), It.IsAny<bool>()))
                .Returns(expectedNodes);

            NodeManager.CacheProvider = cacheProvider.Object;
        }
    }
}